import Vue from 'vue'
import router from './router'
import store from './store'
import { Notify } from 'vant'

import { setDocumentTitle, domTitle } from '@/utils/domUtil'
import { ACCESS_TOKEN, HAS_BANK, DOM_TITLE, REAL_NAME } from '@/store/mutation-types'
import { openconfApi } from "@/api";

const whiteList = ['aboutusList', 'home', 'login', 'register', 'forgetPassword', 'product', 'aboutus', 'aboutusDetails', 'calculator', 'yieldResult', 'productDetails'] // no redirect whitelist、
const needBank = ['withdrawal']
const needUserName = ['withdrawal', 'myBankcard']

router.beforeEach((to, from, next) => {
  let dom_title = domTitle
  if (Vue.ls.get(DOM_TITLE)) {
    dom_title = Vue.ls.get(DOM_TITLE)
  }

  to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${dom_title}`))
  if (whiteList.includes(to.name)) {
    // 在免登录白名单
    next()
  } else {
    if (Vue.ls.get(ACCESS_TOKEN)) {
      // store.dispatch('user/GetInfo').then(res => {
      if (needUserName.includes(to.name) && !Vue.ls.get(REAL_NAME)) {
        // Notify({
        //   type: 'primary', message: '您暂未实名认证,请先实名认证', duration: 1500, onClose: () => {
        //     next({ path: '/user/realname', query: { redirect: to.fullPath } })
        //   }
        // });
        openconfApi({ key: "check_bankchan" }).then(res => {
          if (res.code == 200 && res.data == "new") {
            Notify({
              type: 'primary', message: '您暂未实名认证,请先实名认证', duration: 1500, onClose: () => {
                next({ path: '/user/addBankcard', query: { redirect: to.fullPath } })
              }
            });
          } else {
            Notify({
              type: 'primary', message: '您暂未实名认证,请先实名认证', duration: 1500, onClose: () => {
                next({ path: '/user/realname', query: { redirect: to.fullPath } })
              }
            });
          }

        });
      } else if (needBank.includes(to.name) && !Vue.ls.get(HAS_BANK)) {
        // Notify({
        //   type: 'primary', message: '您暂未拥有银行卡,请先添加银行卡', duration: 1500, onClose: () => {
        //     next({ path: '/user/myBankcard', query: { redirect: to.fullPath } })
        //   }
        // });
        store.dispatch('user/GetInfo').then(res => {
          if (!res.data.alipay_acc && !res.data.bankid) {
            Notify({
              type: 'primary', message: '您暂未拥有银行卡,请先添加银行卡', duration: 1500, onClose: () => {
                next({ path: '/user/myBankcard', query: { redirect: to.fullPath } })
              }
            });
          } else {
            next()
          }
        })

      }
      else {
        next()
      }
      // })
    } else {
      Notify({
        type: 'primary', message: '您暂未登陆,请先登陆', duration: 1500, onClose: () => {
          next({ path: '/login', query: { redirect: to.fullPath } })
        }
      });
    }
  }
})
